﻿Public Class frmOvertimeBR
    Private EmpData As Employee
    Private IsType As String
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    Dim payrollManager As New PayrollManager

    Sub New(ByVal emp As Employee, ByVal type As String)
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        EmpData = emp
        IsType = type
    End Sub
    Private Sub frmOvertimeBR_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()
        Try
            Dim overtimeList As New List(Of Overtime)
            If (EmpData.role = "1" And IsType = "1") Then
                overtimeList = payrollManager.getAllOvertime()
            Else
                overtimeList = payrollManager.getOvertimeEmpId(EmpData.id)
            End If
            For Each overtime As Overtime In overtimeList
                Dim item As ListViewItem
                Dim empInfo As New EmployeeInfo
                If (EmpData.role = "1" And IsType = "1") Then
                    empInfo = New EmployeeInfoManager().retrieveEmployeeInfoByEmployeeId(overtime.employee_id)
                Else
                    empInfo = New EmployeeInfoManager().retrieveEmployeeInfoByEmployeeId(EmpData.id)
                End If
                If Not empInfo Is Nothing Then


                    Dim lastName As String = empInfo.last_name
                    Dim firstName As String = empInfo.first_name

                    Dim toAdd As String = ""
                    If Not lastName Is Nothing Then
                        toAdd += lastName
                        If Not firstName Is Nothing Then
                            toAdd += ", " + firstName
                        End If
                    Else
                        If Not firstName Is Nothing Then
                            toAdd += firstName
                        End If
                    End If


                    item = lvwOvertime.Items.Add("" + toAdd)
                Else
                    item = lvwOvertime.Items.Add("")
                End If

                item.SubItems.Add(overtime.ot_from)
                item.SubItems.Add(overtime.ot_to)
                item.SubItems.Add(overtime.ot_hours)
                item.SubItems.Add(If((overtime.status = "0"), "Unapproved", "Approved"))
                item.SubItems.Add(overtime.id)
            Next
            grpOvertime.Text = "Total Record(s): " + lvwOvertime.Items.Count.ToString()
            validator.resizes(lvwOvertime)
        Catch ex As Exception

        End Try
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpOvertime)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnApprove.Enabled = False
        grpOvertime.Text = "Total Record(s): 0"
        validator.resizes(lvwOvertime)
        Clipboard.Clear()
        btnApprove.Visible = (EmpData.role = "1" And IsType = "1")
        Return 0
    End Function

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        frmOvertimeDE.chkAproveOvertime.Visible = False
        frmOvertimeDE.ShowDialog()
        frmOvertimeBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub lvwOvertime_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvwOvertime.SelectedIndexChanged
        btnEdit.Enabled = (lvwOvertime.SelectedItems.Count = 1)
        btnApprove.Enabled = (lvwOvertime.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwOvertime.SelectedItems.Count > 0)
    End Sub

    Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
        If lvwOvertime.SelectedItems(0).SubItems(4).Text = "Unapproved" Then
            Clipboard.SetText(lvwOvertime.SelectedItems(0).SubItems(5).Text)
            frmOvertimeDE.chkAproveOvertime.Visible = False
            frmOvertimeDE.ShowDialog()
            frmOvertimeBR_Load(vbNull, EventArgs.Empty)
        Else
            MsgBox("The overtime request was already approve.")
        End If
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwOvertime.SelectedItems.Count
                payrollManager.deleteItemOvertime(lvwOvertime.SelectedItems(i - 1).SubItems(5).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmOvertimeBR_Load(vbNull, EventArgs.Empty)
            grpOvertime.Text = "Total Record(s): " + lvwOvertime.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnApprove_Click(sender As System.Object, e As System.EventArgs) Handles btnApprove.Click
        Clipboard.SetText(lvwOvertime.SelectedItems(0).SubItems(5).Text)
        frmOvertimeDE.chkAproveOvertime.Visible = True
        frmOvertimeDE.ShowDialog()
        frmOvertimeBR_Load(vbNull, EventArgs.Empty)
    End Sub
End Class